package expressoes;

import java.util.HashMap;
import java.util.Vector;

public abstract class Binaria implements Expressao {
	
	Expressao exp1, exp2;
	String operador;
	
	@Override
	public String getExpressao(){
		String r1 = exp1.getExpressao();
		String r2 = exp2.getExpressao();
//		
//		if(!(exp1 instanceof Atomica)){
//			r1 = r1;
//		}
//		if(!(exp2 instanceof Atomica)){
//			r2 = "(" + r2 + ")";
//		}
		
		return "(" + r1 + operador + r2 + ")";
	}
	
	@Override
	public Vector<String> getSubExpressoes() {
		Vector<String> sub1 = exp1.getSubExpressoes();
		Vector<String> sub2 = exp2.getSubExpressoes();
		
		for(String str : sub2){
			if(!sub1.contains(str)){
				sub1.add(str);
			}
		}
		sub1.add(getExpressao());
		return sub1;
	}

	@Override
	public abstract String calcula(HashMap<String, String> hash);

	@Override
	public HashMap<String, String> calculaSubExpressoes(
			HashMap<String, String> hash) {
		
		if(!hash.containsKey(exp1.getExpressao())){
			hash = exp1.calculaSubExpressoes(hash);
		}
		if(!hash.containsKey(exp2.getExpressao())){
			hash = exp2.calculaSubExpressoes(hash);
		}
		
		hash.put(getExpressao(), calcula(hash));
		
		return hash;
	}

}
